package com.cloudptt.api.product.func.call.group.fsm.video.trans;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.video.IVideoSubState;
import com.cloudptt.api.product.func.call.group.fsm.video.recv.VideoRecvAgent;
import com.cloudptt.api.product.log.Log;



public class VideoTransPermissionState implements IVideoSubState
{
	private VideoTransAgent agent = null;

	public VideoTransPermissionState(VideoTransAgent agent)
	{
		this.agent = agent;
		Log.d(VideoTransAgent.Log_Key, "currentState is VideoPermissionState");
		
//		this.agent.opAudioMedia(CallAttr.OpMedia_Resume);
		this.agent.opVideoMedia(CallAttr.OpMedia_Resume);
		this.agent.opVideoLocalWinShow(true);
	}
	public int doUserAction(int btnAction)
	{
		return CauseCont.OperateErr_NoAgent;
	}
	public int doUserAction(int btnAction,String infoStr)
	{
		Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " doUserAction btnAction = " + btnAction + " infoStr = " + infoStr);
		int result = CauseCont.OperateCheck_Suc;
		if(CallConst.BtnAction_Trans_End == btnAction)
		{
			result = agent.sendTrans(CallAttr.RtcpMsg_Release, infoStr, 0, 0);
			agent.goToState(CallConst.VideoTransState_PendingRelease);
		}
		return result;
	}
	@Override
	public void receiveRtcpMsg(RtcpMsg msg) {
		// TODO Auto-generated method stub
		Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg msg == null");
			return;
		}
		if(agent == null)
		{
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg agent == null");
			return;
		}

		String title = msg.getTitle();
		if(title.equals(RtcpMsg.Title_MCV1_REVOKED))
		{
			int cause = msg.getCause();
			Log.d(VideoTransAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
			int result = agent.sendTrans(CallAttr.RtcpMsg_Release, "", 0, 0);
			if(result == CallConst.Operate_Suc)
			{
				agent.goToState( CallConst.VideoTransState_NoPermission);
			}
		}
		if(title.equals(RtcpMsg.Title_MCV1_TRANSM_MEDIA_NOTE))
		{
			String num = msg.getVideoTakenNum();//getValue(RtcpMsg.Key_McpttId);
			String jsonStr = msg.getTransInfoJson();
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg num = " + num + " jsonStr = " + jsonStr);
			this.agent.notifyTransUserInfo(num,jsonStr);
		}
		if(title.equals(RtcpMsg.Title_MCV1_TRANSM_MEDIA_END_NOTE))
		{
			String num = msg.getVideoTakenNum();//getValue(RtcpMsg.Key_McpttId);
			String jsonStr = msg.getTransInfoJson();
			Log.d(VideoTransAgent.Log_Key, "receiveRtcpMsg num = " + num + " jsonStr = " + jsonStr);
			this.agent.notifyTransEndUserInfo(num,jsonStr);
		}
		else if(title.equals(RtcpMsg.Title_MCV1_GRANTED))
		{
		}
//		else if(title.equals(RtcpMsg.Title_MCV2_MEDIA_END_REQ))
//		{
//			agent.goToState( CallConst.VideoTransState_NoPermission);
//		}
		else if(title.equals(RtcpMsg.Title_MCV2_TRANSM_MEDIA_RECEP_END_NOTE))
		{
			String num = msg.getVideoTakenNum();//getValue(RtcpMsg.Key_McpttId);
//			String jsonStr = msg.getTransInfoJson();
			Log.d(VideoRecvAgent.Log_Key, "receiveRtcpMsg  num = " + num);
			this.agent.notifyMediaRecepEnd(num);
		}
	}
	
	public void destory()
	{
//		this.agent.opAudioMedia(CallAttr.OpMedia_Pause);
//		this.agent.opVideoMedia(CallAttr.OpMedia_Pause);
		this.agent = null;
	}
	
	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return CallConst.VideoTransState_Permission;
	}
}
